﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using KeepAnimals.DAL;
namespace KeepAnimals.BLL
{
    // Classe pour gerer les différents animaux du chenil 
    public class Animal
    {
        //Constante pour les variables 0 pour un chat // 1 pour un chien  // rajouter animaux si besoin 
        public const int ANIMAL_CAT = 0;
        public const int ANIMAL_DOG = 1;

        //Déclaration des propriétés, toutes ses propriétés sont stocké dans la base "animal"
        public int Id { get; set; }
        public int IdCategory { get; set; }
        public int IdBox { get; set; }
        public int Age { get; set; }
        public double  Weight { get; set; }
        public double Price { get; set; }
        public string  Name { get; set; }
        public string  State { get; set; }
        public string Reason { get; set; }
        public string  Description { get; set; }
        public DateTime DateArrival { get; set; }
        public DateTime DateDeparture { get; set; }
        public int  AtDress { get; set; }
        // doit etre elever / a domestiquer 
        //private static Connection con;
        public Animal()
        {
            //con = Connection.getInstance();

        }
        public Animal(string nameAnimal)
        {
            Name = nameAnimal;
            
        }
        //Fonction pour ajouter un animal dans la base de donnée depuis le formulaire de la vue, elle renvoi l'id du dernier animal mis dans la base. 
        public int AddAnimalInDatabase()
        {
            MySqlCommand command = Connection.getInstanceOfCommand();
            //MySqlCommand command = Connection.connection.CreateCommand();
            command.CommandText = "INSERT INTO animals(IdCategory,IdBox,Age,Weight,Price,Name,State,Reason,Description,DateArrival,DateDeparture,AtDress)VALUES(?IdCategory,?IdBox,?Age,?Weight,?Price,?Name,?State,?Reason,?Description,?DateArrival,?DateDeparture,?AtDress);";
            command.Parameters.AddWithValue("?IdCategory", this.IdCategory);
            command.Parameters.AddWithValue("?IdBox", this.IdBox);
            command.Parameters.AddWithValue("?Age", this.Age);
            command.Parameters.AddWithValue("?Weight", this.Weight);
            command.Parameters.AddWithValue("?Price", this.Price);
            command.Parameters.AddWithValue("?Name", this.Name);
            command.Parameters.AddWithValue("?State", this.State);
            command.Parameters.AddWithValue("?Reason", this.Reason);
            command.Parameters.AddWithValue("?Description", this.Description);
            command.Parameters.AddWithValue("?DateArrival", this.DateArrival);
            command.Parameters.AddWithValue("?DateDeparture", this.DateDeparture);
            command.Parameters.AddWithValue("?AtDress", this.AtDress);
            Connection.AddUpdateCommandInDatabase(command);
            return Connection.ReturnLastId("animals");
        }
        /// <summary>
        /// Updates the animal in database.
        /// </summary>
        public void UpdateAnimalInDatabase()
        {
            MySqlCommand command = Connection.getInstanceOfCommand();
            //MySqlCommand command = Connection.connection.CreateCommand();

            command.CommandText = "UPDATE animals SET Name = ?Name,State=?State,Reason=?Reason,Description=?Description,DateArrival=?DateArrival,DateDeparture=?DateDeparture,AtDress=?AtDress WHERE id ="+this.Id+";";
            //command.Parameters.AddWithValue("?Id", this.Id);
            command.Parameters.AddWithValue("?IdCategory", this.IdCategory);
            command.Parameters.AddWithValue("?IdBox", this.IdBox);
            command.Parameters.AddWithValue("?Age", this.Age);
            command.Parameters.AddWithValue("?Weight", this.Weight);
            command.Parameters.AddWithValue("?Price", this.Price);
            command.Parameters.AddWithValue("?Name", this.Name);
            command.Parameters.AddWithValue("?State", this.State);
            command.Parameters.AddWithValue("?Reason", this.Reason);
            command.Parameters.AddWithValue("?Description", this.Description);
            command.Parameters.AddWithValue("?DateArrival", this.DateArrival);
            command.Parameters.AddWithValue("?DateDeparture", this.DateDeparture);
            command.Parameters.AddWithValue("?AtDress", this.AtDress);
            Connection.AddUpdateCommandInDatabase(command);
            Console.WriteLine(command.CommandText);
        }
        /// <summary>
        /// Gets the animal by identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public static Animal GetAnimalById(int id)
        {
            
            Connection.OpenConnection();
            Animal tempAnimal = new Animal();
            MySqlCommand command = Connection.getInstanceOfCommand();
            command.CommandText = "SELECT * FROM animals WHERE Id = "+id+";";
            MySqlDataReader rdr = Connection.getAllData(command);
            try
            {
                while (rdr.Read())
                {
                    if (!rdr.IsDBNull(0))
                        tempAnimal.Id = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        tempAnimal.IdCategory = rdr.GetInt32(1);
                    if (!rdr.IsDBNull(2))
                        tempAnimal.IdBox = rdr.GetInt32(2);
                    if (!rdr.IsDBNull(3))
                        tempAnimal.Age = rdr.GetInt32(3);
                    if (!rdr.IsDBNull(4))
                        tempAnimal.Weight = rdr.GetDouble(4);
                    if (!rdr.IsDBNull(5))
                        tempAnimal.Price = rdr.GetDouble(5);
                    if (!rdr.IsDBNull(6))
                        tempAnimal.Name = rdr.GetString(6);
                    if (!rdr.IsDBNull(7))
                        tempAnimal.State = rdr.GetString(7);
                    if (!rdr.IsDBNull(8))
                        tempAnimal.Reason = rdr.GetString(8);
                    if (!rdr.IsDBNull(9))
                        tempAnimal.Description = rdr.GetString(9);
                    if (!rdr.IsDBNull(10))
                        tempAnimal.DateArrival = rdr.GetDateTime(10);
                    if (!rdr.IsDBNull(11))
                        tempAnimal.DateDeparture = rdr.GetDateTime(11);
                    if (!rdr.IsDBNull(12))
                        tempAnimal.AtDress = rdr.GetInt32(12);
                }
            }
            finally
            {
                rdr.Close();
                Connection.CloseConnection();
            }

            return tempAnimal;
        }

        public static Animal GetAnimalByIdBox(int idBox)
        {

            Connection.OpenConnection();
            Animal tempAnimal = new Animal();
            MySqlCommand command = Connection.getInstanceOfCommand();
            command.CommandText = "SELECT * FROM animals WHERE IdBox = " + idBox + ";";
            MySqlDataReader rdr = Connection.getAllData(command);
          
            try
            {
               
                while (rdr.Read())
                {
                   if (!rdr.IsDBNull(1))
                    {
                         tempAnimal = AnimalFactory.CreateAnimal(rdr.GetInt32(1));
                    }   
                    if (!rdr.IsDBNull(0))
                        tempAnimal.Id = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        tempAnimal.IdCategory = rdr.GetInt32(1);
                    if (!rdr.IsDBNull(2))
                        tempAnimal.IdBox = rdr.GetInt32(2);
                    if (!rdr.IsDBNull(3))
                        tempAnimal.Age = rdr.GetInt32(3);
                    if (!rdr.IsDBNull(4))
                        tempAnimal.Weight = rdr.GetDouble(4);
                    if (!rdr.IsDBNull(5))
                        tempAnimal.Price = rdr.GetDouble(5);
                    if (!rdr.IsDBNull(6))
                        tempAnimal.Name = rdr.GetString(6);
                    if (!rdr.IsDBNull(7))
                        tempAnimal.State = rdr.GetString(7);
                    if (!rdr.IsDBNull(8))
                        tempAnimal.Reason = rdr.GetString(8);
                    if (!rdr.IsDBNull(9))
                        tempAnimal.Description = rdr.GetString(9);
                    if (!rdr.IsDBNull(10))
                        tempAnimal.DateArrival = rdr.GetDateTime(10);
                    if (!rdr.IsDBNull(11))
                        tempAnimal.DateDeparture = rdr.GetDateTime(11);
                    if (!rdr.IsDBNull(12))
                        tempAnimal.AtDress = rdr.GetInt32(12);
                }
            }
            finally
            {
                rdr.Close();
                Connection.CloseConnection();
            }

            return tempAnimal;
        }

           

        //Fonction pour envoyer a la vu la liste de l'animal selection , exemple chat / chien , on passe un id de la categorie animal
        public static  List<Animal> GetListOfAllAnimals()
        {
            Connection.OpenConnection();
            List<Animal> animals = new List<Animal>();
            MySqlCommand command = Connection.getInstance().CreateCommand();
            command.CommandText = "SELECT * FROM animals";
            MySqlDataReader rdr = Connection.getAllData(command);

            try
            {
                while (rdr.Read())
                {
                    Animal tempAnimal = new Animal();
                    if(!rdr.IsDBNull(0))
                        tempAnimal.Id = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        tempAnimal.IdCategory = rdr.GetInt32(1);
                    if(!rdr.IsDBNull(2))
                        tempAnimal.IdBox = rdr.GetInt32(2);
                    if (!rdr.IsDBNull(3))
                        tempAnimal.Age = rdr.GetInt32(3);
                    if (!rdr.IsDBNull(4))
                        tempAnimal.Weight = rdr.GetDouble(4);
                    if (!rdr.IsDBNull(5))
                        tempAnimal.Price = rdr.GetDouble(5);
                    if (!rdr.IsDBNull(6))
                        tempAnimal.Name = rdr.GetString(6);
                    if (!rdr.IsDBNull(7))
                        tempAnimal.State = rdr.GetString(7);
                    if (!rdr.IsDBNull(8))
                        tempAnimal.Reason = rdr.GetString(8);
                    if (!rdr.IsDBNull(9))
                        tempAnimal.Description = rdr.GetString(9);
                    if (!rdr.IsDBNull(10))
                        tempAnimal.DateArrival = rdr.GetDateTime(10);
                    if (!rdr.IsDBNull(11))
                        tempAnimal.DateDeparture = rdr.GetDateTime(11);
                    if (!rdr.IsDBNull(12))
                        tempAnimal.AtDress = rdr.GetInt32(12);
                    animals.Add(tempAnimal);
                }
            }
            finally
            {
                rdr.Close();
                Connection.CloseConnection();
            }

            return animals;
        }

        public static Animal DeleteAnimalById(int id)
        {

            Connection.OpenConnection();
            Animal tempAnimal = new Animal();
            MySqlCommand command = Connection.getInstanceOfCommand();
            command.CommandText = "DELETE FROM animals WHERE Id = " + id + ";";
            MySqlDataReader rdr = Connection.getAllData(command);
            try
            {
                while (rdr.Read())
                {
                    if (!rdr.IsDBNull(0))
                        tempAnimal.Id = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        tempAnimal.IdCategory = rdr.GetInt32(1);
                    if (!rdr.IsDBNull(2))
                        tempAnimal.IdBox = rdr.GetInt32(2);
                    if (!rdr.IsDBNull(3))
                        tempAnimal.Age = rdr.GetInt32(3);
                    if (!rdr.IsDBNull(4))
                        tempAnimal.Weight = rdr.GetDouble(4);
                    if (!rdr.IsDBNull(5))
                        tempAnimal.Price = rdr.GetDouble(5);
                    if (!rdr.IsDBNull(6))
                        tempAnimal.Name = rdr.GetString(6);
                    if (!rdr.IsDBNull(7))
                        tempAnimal.State = rdr.GetString(7);
                    if (!rdr.IsDBNull(8))
                        tempAnimal.Reason = rdr.GetString(8);
                    if (!rdr.IsDBNull(9))
                        tempAnimal.Description = rdr.GetString(9);
                    if (!rdr.IsDBNull(10))
                        tempAnimal.DateArrival = rdr.GetDateTime(10);
                    if (!rdr.IsDBNull(11))
                        tempAnimal.DateDeparture = rdr.GetDateTime(11);
                    if (!rdr.IsDBNull(12))
                        tempAnimal.AtDress = rdr.GetInt32(12);
                }
            }
            finally
            {
                rdr.Close();
                Connection.CloseConnection();
            }

            return tempAnimal;
        }

        public static List<Animal> GetAnimalByIdCategory(int IdCategory)
        {

            Connection.OpenConnection();
            List<Animal> animals = new List<Animal>();
            MySqlCommand command = Connection.getInstanceOfCommand();
            command.CommandText = "SELECT * FROM animals WHERE IdCategory = " + IdCategory + ";";
            MySqlDataReader rdr = Connection.getAllData(command);

            try
            {

                while (rdr.Read())
                {
                    Animal tempAnimal = new Animal();
                    if (!rdr.IsDBNull(0))
                        tempAnimal.Id = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        tempAnimal.IdCategory = rdr.GetInt32(1);
                    if (!rdr.IsDBNull(2))
                        tempAnimal.IdBox = rdr.GetInt32(2);
                    if (!rdr.IsDBNull(3))
                        tempAnimal.Age = rdr.GetInt32(3);
                    if (!rdr.IsDBNull(4))
                        tempAnimal.Weight = rdr.GetDouble(4);
                    if (!rdr.IsDBNull(5))
                        tempAnimal.Price = rdr.GetDouble(5);
                    if (!rdr.IsDBNull(6))
                        tempAnimal.Name = rdr.GetString(6);
                    if (!rdr.IsDBNull(7))
                        tempAnimal.State = rdr.GetString(7);
                    if (!rdr.IsDBNull(8))
                        tempAnimal.Reason = rdr.GetString(8);
                    if (!rdr.IsDBNull(9))
                        tempAnimal.Description = rdr.GetString(9);
                    if (!rdr.IsDBNull(10))
                        tempAnimal.DateArrival = rdr.GetDateTime(10);
                    if (!rdr.IsDBNull(11))
                        tempAnimal.DateDeparture = rdr.GetDateTime(11);
                    if (!rdr.IsDBNull(12))
                        tempAnimal.AtDress = rdr.GetInt32(12);
                    animals.Add(tempAnimal);
                }
            }
            finally
            {
                rdr.Close();
                Connection.CloseConnection();
            }

            return animals;
        }


        public static Animal GetAnimalByName(string name)
        {

            Connection.OpenConnection();
            Animal tempAnimal = new Animal();
            MySqlCommand command = Connection.getInstanceOfCommand();
            command.CommandText = "SELECT * FROM animals WHERE Name = '" + name + "';";
            MySqlDataReader rdr = Connection.getAllData(command);

            try
            {

                while (rdr.Read())
                {
                    if (!rdr.IsDBNull(1))
                    {
                        tempAnimal = AnimalFactory.CreateAnimal(rdr.GetInt32(1));
                    }
                    if (!rdr.IsDBNull(0))
                        tempAnimal.Id = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        tempAnimal.IdCategory = rdr.GetInt32(1);
                    if (!rdr.IsDBNull(2))
                        tempAnimal.IdBox = rdr.GetInt32(2);
                    if (!rdr.IsDBNull(3))
                        tempAnimal.Age = rdr.GetInt32(3);
                    if (!rdr.IsDBNull(4))
                        tempAnimal.Weight = rdr.GetDouble(4);
                    if (!rdr.IsDBNull(5))
                        tempAnimal.Price = rdr.GetDouble(5);
                    if (!rdr.IsDBNull(6))
                        tempAnimal.Name = rdr.GetString(6);
                    if (!rdr.IsDBNull(7))
                        tempAnimal.State = rdr.GetString(7);
                    if (!rdr.IsDBNull(8))
                        tempAnimal.Reason = rdr.GetString(8);
                    if (!rdr.IsDBNull(9))
                        tempAnimal.Description = rdr.GetString(9);
                    if (!rdr.IsDBNull(10))
                        tempAnimal.DateArrival = rdr.GetDateTime(10);
                    if (!rdr.IsDBNull(11))
                        tempAnimal.DateDeparture = rdr.GetDateTime(11);
                    if (!rdr.IsDBNull(12))
                        tempAnimal.AtDress = rdr.GetInt32(12);
                }
            }
            finally
            {
                rdr.Close();
                Connection.CloseConnection();
            }

            return tempAnimal;
        }
        
    }
}
